package interfaceGrafica;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import logica.main.MainGrafico;
import logica.pessoa.Locador;
import logica.util.Data;
import logica.util.TipoDeCadastro;
import logica.util.TipoDeEntradaInvalida;
import persistencia.GeradorDeLogs;
import persistencia.MecanismoDePersistenciaSeguro;
import util.Constantes;
import util.Endereco;
import util.TipoDeLogradouro;
import util.UF;


@SuppressWarnings("serial")
public class InterfaceLocador extends javax.swing.JPanel {

    /**
	 * 
	 */
	
	private List<Locador> usuarios;
    private Locador usuarioAtual;
    private static MecanismoDePersistenciaSeguro mecanismoDePersistenciaUsuarios;
    private static GeradorDeLogs geradorDeLog;

    public InterfaceLocador() {
        try {
            geradorDeLog = new GeradorDeLogs();
        } catch (Exception e) {
        }
        
        try {
			geradorDeLog.escreveLog(" O usuario "+MainGrafico.getUsuarioLogado().getLogin()+" entrou no gereciamento de locadores");
		} catch (Exception e) {
		
		}
        
        lerDados();
        initComponents();
        for ( UF estado : UF.values()){
            jComboBox2.addItem(estado);
        }
        for ( TipoDeLogradouro tipoDeLogradouro : TipoDeLogradouro.values()){
            jComboBox1.addItem(tipoDeLogradouro);
        }
        for ( Locador usuario : usuarios ){
        	if ( !usuario.getEhGerente() ){
                list1.add(usuario.getNome()+" # "+usuario.getLogin());        		
        	}
        }
        jButton6.setEnabled(false);
        jButton3.setEnabled(false);
        this.setVisible(true);
    }

    public void limparCampos(){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField7.setText("");
        jTextField8.setText("");
        jTextField9.setText("");
        jTextField10.setText("");
        jTextField11.setText("");
        jTextField12.setText("");
        jTextField13.setText("");
        jTextField14.setText("");
        jTextField15.setText("");
        jPasswordField1.setText("");
        jLabel2.setForeground(Color.BLACK);
        jLabel3.setForeground(Color.BLACK);
        jLabel4.setForeground(Color.BLACK);
        jLabel6.setForeground(Color.BLACK);
        jLabel7.setForeground(Color.BLACK);
        jLabel8.setForeground(Color.BLACK);
        jLabel9.setForeground(Color.BLACK);
        jLabel12.setForeground(Color.BLACK);
        jLabel13.setForeground(Color.BLACK);
        jLabel14.setForeground(Color.BLACK);
        jLabel16.setForeground(Color.BLACK);
        jLabel17.setForeground(Color.BLACK);
        jLabel18.setForeground(Color.BLACK);
        jLabel19.setForeground(Color.BLACK);
    }


    @SuppressWarnings("unchecked")
	private boolean lerDados(){
        try{
            mecanismoDePersistenciaUsuarios = new MecanismoDePersistenciaSeguro("arquivos/locadores.ser");
            usuarios = (ArrayList<Locador>) mecanismoDePersistenciaUsuarios.recuperaDoArquivo();
            if(usuarios == null){
            	usuarios = new ArrayList<Locador>();
            }
            return true;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Houve um problema no carregamento do banco de dados do Sistema.", null, JOptionPane.ERROR_MESSAGE);
            MainGrafico.entrarNaArea(MainGrafico.getMenuCorrente(), Constantes.INTERFACE_TAMANHO_MENU[0], Constantes.INTERFACE_TAMANHO_MENU[1]);
            MainGrafico.sairDaArea(this);
            return false;
        }
    }

    @SuppressWarnings("unchecked")
	private void persistirDados(){
        try{
           geradorDeLog.escreveLog("Gravando dados");
           mecanismoDePersistenciaUsuarios.persisteNoArquivo((ArrayList) this.usuarios);
        }catch(Exception e){
        }
    }

    @SuppressWarnings("deprecation")
	private void cadastraLocador(Locador locador, TipoDeCadastro tipoDeCadastro, String mensagemSucesso, String tituloSucesso, String mensagemFalha, String tituloFalha){
        jLabel2.setForeground(Color.BLACK);
        jLabel3.setForeground(Color.BLACK);
        jLabel4.setForeground(Color.BLACK);
        jLabel6.setForeground(Color.BLACK);
        jLabel7.setForeground(Color.BLACK);
        jLabel8.setForeground(Color.BLACK);
        jLabel9.setForeground(Color.BLACK);
        jLabel12.setForeground(Color.BLACK);
        jLabel13.setForeground(Color.BLACK);
        jLabel14.setForeground(Color.BLACK);
        jLabel16.setForeground(Color.BLACK);
        jLabel17.setForeground(Color.BLACK);
        jLabel18.setForeground(Color.BLACK);
        jLabel19.setForeground(Color.BLACK);
        List<TipoDeEntradaInvalida> entradasInvalidas = new ArrayList<TipoDeEntradaInvalida>();
       
        String login = jTextField15.getText();
        boolean loginValido = true;
        try {
            for (Locador usuario : usuarios) {
                if ( usuario.getEhGerente() ){
                    continue;
                }
                if (login.equals(usuario.getLogin())) {
                    loginValido = false;
                }
            }
            if (loginValido) {
                locador.setLogin(login);
            } else {
                throw new Exception();
            }
            locador.setLogin(jTextField15.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.LOGIN);
        }

        try {
            locador.setSenha(jPasswordField1.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.SENHA);
        }

        try {
            locador.setNome(jTextField1.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.NOME);
        }

        try {
            locador.setTelefone(jTextField13.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.TELEFONE);
        }

        try {
            locador.setEmail(jTextField2.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.EMAIL);
        }

        String CPF = jTextField3.getText();
        boolean cpfValido = true;
        try {
            for (Locador usuario : usuarios) {
                if ( usuario.getEhGerente() ){
                    continue;
                }
                if (CPF.replace("-", "").replace(".", "").equals((usuario.getCPF().replace("-", "").replace(".", "")))) {
                    cpfValido = false;
                }
            }
            if (cpfValido) {
                locador.setCPF(CPF);
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CPF);
        }

        String RG = jTextField4.getText();
        boolean rgValido = true;
        try {
            for (Locador usuario : usuarios) {
                if ( usuario.getEhGerente() ){
                    continue;
                }
                if (RG.equals(usuario.getRG())) {
                    rgValido = false;
                }
            }
            if (rgValido) {
                locador.setRG(RG);
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.RG);
        }

        try {
            int dia = Integer.parseInt(jTextField14.getText().split("/")[0]);
            int mes = Integer.parseInt(jTextField14.getText().split("/")[1]);
            int ano = Integer.parseInt(jTextField14.getText().split("/")[2]);
            locador.setDataDeNascimento(new Data(dia, mes, ano));
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.DATA);
        }

        try {
            locador.setNaturalidade(jTextField5.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.NATURALIDADE);
        }

        Endereco endereco = new Endereco();
        try {
            endereco.setTipoDeLogradouro((TipoDeLogradouro) jComboBox1.getSelectedItem());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.TIPO_DE_LOGRADOURO);
        }
        try {
            endereco.setLogradouro(jTextField7.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.LOGRADOURO);
        }
        try {
            endereco.setNumero(Integer.parseInt(jTextField8.getText()));
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.NUMERO_ENDERECO);
        }
        try {
            endereco.setObs(jTextField11.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.OBSERVACAO);
        }
        try {
            endereco.setBairro(jTextField9.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.BAIRRO);
        }
        try {
            endereco.setCidade(jTextField10.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CIDADE);
        }
        try {
            endereco.setEstado((UF) jComboBox2.getSelectedItem());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.ESTADO);
        }
        try {
            endereco.setCep(jTextField12.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CEP);
        }

        try {
            locador.setEndereco(endereco);
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.ENDERECO);
        }

        if ( entradasInvalidas.size() == 0 ){
            usuarios.add(locador);
            list1.add(locador.getNome()+" # "+locador.getLogin());
            list1.select(list1.getItemCount()-1);
            JOptionPane.showMessageDialog(null, mensagemSucesso, tituloSucesso,JOptionPane.PLAIN_MESSAGE);
            list1.select(-1);
            limparCampos();
        }
        else{
            if (tipoDeCadastro == TipoDeCadastro.EDICAO) {
                list1.add(locador.getNome() + " # " + locador.getLogin());
                usuarios.add(locador);
                
                try {
        			geradorDeLog.escreveLog("Informacoes do locador "+locador.getNome()+" foram alteradas.");
        		} catch (Exception e) {
        		
        		}
                
                list1.select(list1.getItemCount()-1);
            }
            if (!cpfValido || !rgValido || !loginValido) {
                JOptionPane.showMessageDialog(null, "Nao pode haver duplicacao de CPF, RG ou logins.", tituloFalha, JOptionPane.ERROR_MESSAGE);
            }
            else{
                JOptionPane.showMessageDialog(null, mensagemFalha, tituloFalha,JOptionPane.ERROR_MESSAGE);
            }
            for ( TipoDeEntradaInvalida entradaInvalida : entradasInvalidas ){
                if ( entradaInvalida == TipoDeEntradaInvalida.NOME ){
                    jLabel2.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.TELEFONE ){
                    jLabel3.setForeground(Color.RED);
                    jTextField13.setText("(XX) XXXX-XXXX");
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.EMAIL ){
                    jLabel4.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.CPF ){
                    jLabel6.setForeground(Color.RED);
                    jTextField3.setText("XXX.XXX.XXX-XX");
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.RG ){
                    jLabel7.setForeground(Color.RED);
                    jTextField4.setText("XXXXXXXX (7 a 12)");
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.DATA ){
                    jLabel8.setForeground(Color.RED);
                    jTextField14.setText("DD/MM/AAAA");
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.NATURALIDADE ){
                    jLabel9.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.NUMERO_ENDERECO ){
                    jLabel12.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.BAIRRO ){
                    jLabel13.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.CIDADE ){
                    jLabel14.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.ESTADO ){
                    jLabel15.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.OBSERVACAO ){
                    jLabel16.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.CEP ){
                    jLabel17.setForeground(Color.RED);
                    jTextField12.setText("XXXXXXXX");
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.LOGIN ){
                    jLabel18.setForeground(Color.RED);
                }
                else if ( entradaInvalida == TipoDeEntradaInvalida.SENHA ){
                    jLabel19.setForeground(Color.RED);
                }
            }
        }

    }
   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField7 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        scrollPane1 = new java.awt.ScrollPane();
        list1 = new java.awt.List();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jSeparator4 = new javax.swing.JSeparator();
        jButton6 = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        jTextField15 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jLabel1.setText("Locadores");

        jLabel2.setText("Nome:");

        jLabel3.setText("Telefone:");

        jLabel4.setText("Email:");

        jLabel6.setText("CPF:");

        jLabel7.setText("RG:");

        jLabel8.setDisplayedMnemonic('t');
        jLabel8.setText("Data de Nascimento:");

        jLabel9.setText("Naturalidade:");

        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Excluir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Voltar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("OK");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel10.setText("Consultar:");

        jLabel11.setText("Endereco");

        jLabel12.setText("No.:");

        jLabel13.setText("Bairro:");

        jLabel14.setText("Cidade:");

        jLabel15.setText("UF:");

        jLabel16.setText("Observacao:");

        jLabel17.setText("CEP:");

        list1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                list1ItemStateChanged(evt);
            }
        });
        list1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                list1FocusGained(evt);
            }
        });
        scrollPane1.add(list1);

        jButton6.setText("Salvar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel18.setText("Login:");

        jLabel19.setText("Senha:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(18, 18, 18)
                                                .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel17)
                                                .addGap(18, 18, 18)
                                                .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel18)
                                                .addGap(18, 18, 18)
                                                .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel19)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel8)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextField14, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel16)
                                                .addGap(404, 404, 404))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel6)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField3))
                                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                        .addComponent(jLabel3)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel4)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel7)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel11)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel13)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jLabel14)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField10, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel12)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jTextField8))
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel15)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                            .addComponent(jTextField11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(22, 22, 22)
                                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton1)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)))
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton5))
                                    .addComponent(scrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton5)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel6)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(16, 16, 16)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel12)
                                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel13)
                                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel14)
                                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel15)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel16)
                                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel17)
                                    .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel18)
                                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel19))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButton4)
                                    .addComponent(jButton2)
                                    .addComponent(jButton3)
                                    .addComponent(jButton1)
                                    .addComponent(jButton6)))
                            .addComponent(scrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        limparCampos();
        list1.select(-1);
        jButton6.setEnabled(false);
        jButton1.setEnabled(true);
        jButton3.setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Locador novo = new Locador();
        cadastraLocador(novo, TipoDeCadastro.NOVA_ENTRADA, "Cadastro efetuado com sucesso.", "Cadastro realizado", "O cadastro falhou. Os campos errados estao em destaque.", "Cadastro falhou");
        persistirDados();        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        limparCampos();
        if ( usuarioAtual != null ){
            if ( usuarioAtual.getEhGerente() ){
                JOptionPane.showMessageDialog(null, "O gerente nao pode ser excluido!", "Locador nao removido", JOptionPane.PLAIN_MESSAGE);
                usuarioAtual = null;
                jButton6.setEnabled(false);
                jButton1.setEnabled(true);
                jButton3.setEnabled(false);
                list1.select(-1);
            }
            else {
                list1.remove(usuarioAtual.getNome() + " # " + usuarioAtual.getLogin());
                usuarios.remove(usuarioAtual);
                try {
        			geradorDeLog.escreveLog("Informacoes do locador "+usuarioAtual.getNome()+" foram exluidas do sistema.");
        		} catch (Exception e) {
        		}
        		
                usuarioAtual = null;
                jButton6.setEnabled(false);
                jButton1.setEnabled(true);
                jButton3.setEnabled(false);
                list1.select(-1);
                JOptionPane.showMessageDialog(null, "Locador excluido com sucesso!", "Locador removido", JOptionPane.PLAIN_MESSAGE);
                persistirDados();
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Voce deve selecionar um locador para ser excluido!", "Locador nao especificado", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void selecionarLocador( ){
        limparCampos();
        String selecionado = list1.getItems()[list1.getSelectedIndex()];
        String LoginDoLocador = selecionado.split(" # ")[1];
        for (Locador locador : usuarios) {
            if ( locador.getLogin().equals(LoginDoLocador) ) {
                usuarioAtual = locador;
                jButton6.setEnabled(true);
                jButton1.setEnabled(false);
                jButton3.setEnabled(true);
                jTextField1.setText(locador.getNome());
                jTextField13.setText(locador.getTelefone());
                jTextField2.setText(locador.getEmail());
                jTextField3.setText(locador.getCPF());
                jTextField4.setText(locador.getRG());
                jTextField14.setText(locador.getDataDeNascimento().getData());
                jTextField5.setText(locador.getNaturalidade());
                jComboBox1.setSelectedItem(locador.getEndereco().getTipoDeLogradouro());
                jTextField7.setText(locador.getEndereco().getLogradouro());
                jTextField8.setText(new Integer(locador.getEndereco().getNumero()).toString());
                jTextField9.setText(locador.getEndereco().getBairro());
                jTextField10.setText(locador.getEndereco().getCidade());
                jComboBox2.setSelectedItem(locador.getEndereco().getEstado());
                jTextField11.setText(locador.getEndereco().getObs());
                jTextField12.setText(locador.getEndereco().getCep());
                jTextField15.setText(locador.getLogin());
                jPasswordField1.setText(locador.getSenha());
                break;
            }
        }
    }

    private void list1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_list1ItemStateChanged
        selecionarLocador();
    }//GEN-LAST:event_list1ItemStateChanged

    private void list1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_list1FocusGained
        
    }//GEN-LAST:event_list1FocusGained

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        String parametroDeConsulta = jTextField6.getText();
        
        String[] consultados = list1.getItems();
        
        for ( int i=0; i<consultados.length; i++ ){
            if ( (consultados[i].toLowerCase()).
                    contains(parametroDeConsulta.toLowerCase()) ){
                list1.select(i);
                jTextField6.setText("");
                selecionarLocador();
                break;
            }
        }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

        if (!usuarioAtual.getEhGerente()) {
            list1.remove(usuarioAtual.getNome() + " # " + usuarioAtual.getLogin());
            usuarios.remove(usuarioAtual);
            cadastraLocador(usuarioAtual, TipoDeCadastro.EDICAO, "Edicao realizada com sucesso.", "Edicao realizada", "A edicao falhou. Verifique campos em destaque.", "Edicao nao realizada");
            jButton6.setEnabled(false);
            jButton3.setEnabled(false);
            jButton1.setEnabled(true);
            list1.select(-1);
            persistirDados();
        }
        else{
            JOptionPane.showMessageDialog(null, "Nao eh permitido editar o gerente aqui!", "Edicao nao permitida", JOptionPane.ERROR_MESSAGE);
            jButton6.setEnabled(false);
            jButton3.setEnabled(false);
            jButton1.setEnabled(true);
            list1.select(-1);
        }

    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        MainGrafico.entrarNaArea(MainGrafico.getMenuCorrente(), Constantes.INTERFACE_TAMANHO_MENU[0], Constantes.INTERFACE_TAMANHO_MENU[1]);
        MainGrafico.sairDaArea(this);
    }//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private java.awt.List list1;
    private java.awt.ScrollPane scrollPane1;
    // End of variables declaration//GEN-END:variables

}
